﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.SqlClient;
using Entities;

namespace DataAccessLayer
{
    public class GoiKhoaDAL : DBConnection
    {
        public List<GoiKhoaEntity> getAll()
        {
            List<GoiKhoaEntity> lst = new List<GoiKhoaEntity>();
            try
            {
                MySqlCommand cmd = new MySqlCommand("goi_khoa_getAll", con);
                cmd.CommandType = CommandType.StoredProcedure;

                if (con.State != ConnectionState.Open)
                    con.Open();
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                while (dr.Read())
                {
                    GoiKhoaEntity obj = new GoiKhoaEntity();
                    obj.MaGoiKhoa = Convert.ToInt32(dr["MaGoiKhoa"]);
                    obj.TenGoiKhoa = Convert.ToString(dr["TenGoiKhoa"]);
                    obj.MaLopHoc = Convert.ToInt32(dr["MaLopHoc"]);
                    obj.SoBuoi = Convert.ToInt32(dr["SoBuoi"]);
                    obj.ThoiGian = Convert.ToString(dr["ThoiGian"]);
                    obj.HocPhiCoBan = Convert.ToDecimal(dr["HocPhiCoBan"]);
                    obj.MoTa = Convert.ToString(dr["MoTa"]);
                    lst.Add(obj);
                }
                dr.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return lst;
        }

        public GoiKhoaEntity getById(int _id)
        {
            GoiKhoaEntity obj = new GoiKhoaEntity();
            try
            {
                MySqlCommand cmd = new MySqlCommand("goi_khoa_getById", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("maGoiKhoa", _id);

                if (con.State != ConnectionState.Open)
                    con.Open();
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                if (dr.Read())
                {
                    obj.MaGoiKhoa = Convert.ToInt32(dr["MaGoiKhoa"]);
                    obj.TenGoiKhoa = Convert.ToString(dr["TenGoiKhoa"]);
                    obj.MaLopHoc = Convert.ToInt32(dr["MaLopHoc"]);
                    obj.SoBuoi = Convert.ToInt32(dr["SoBuoi"]);
                    obj.ThoiGian = Convert.ToString(dr["ThoiGian"]);
                    obj.HocPhiCoBan = Convert.ToDecimal(dr["HocPhiCoBan"]);
                    obj.MoTa = Convert.ToString(dr["MoTa"]);
                }
                dr.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return obj;
        }

        public List<GoiKhoaEntity> getByLopHocId(int _id)
        {
            List<GoiKhoaEntity> lst = new List<GoiKhoaEntity>();
            try
            {
                MySqlCommand cmd = new MySqlCommand("goi_khoa_getsByLopHocId", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("maLopHoc", _id);

                if (con.State != ConnectionState.Open)
                    con.Open();
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                while (dr.Read())
                {
                    GoiKhoaEntity obj = new GoiKhoaEntity();
                    obj.MaGoiKhoa = Convert.ToInt32(dr["MaGoiKhoa"]);
                    obj.TenGoiKhoa = Convert.ToString(dr["TenGoiKhoa"]);
                    obj.MaLopHoc = Convert.ToInt32(dr["MaLopHoc"]);
                    obj.SoBuoi = Convert.ToInt32(dr["SoBuoi"]);
                    obj.ThoiGian = Convert.ToString(dr["ThoiGian"]);
                    obj.HocPhiCoBan = Convert.ToDecimal(dr["HocPhiCoBan"]);
                    obj.MoTa = Convert.ToString(dr["MoTa"]);
                    lst.Add(obj);
                }
                dr.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return lst;
        }

        public int addGoiKhoa(GoiKhoaEntity _obj)
        {
            int result = 0;
            try
            {
                if (_obj.MaGoiKhoa != -1)
                    throw new Exception("Goi Khoa Da Ton Tai");

                MySqlCommand cmd = new MySqlCommand("goi_khoa_insert", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("tenGoiKhoa", _obj.TenGoiKhoa);
                cmd.Parameters.AddWithValue("maLopHoc", _obj.MaLopHoc);
                cmd.Parameters.AddWithValue("soBuoi", _obj.SoBuoi);
                cmd.Parameters.AddWithValue("thoiGian", _obj.ThoiGian);
                cmd.Parameters.AddWithValue("hocPhiCoBan", _obj.HocPhiCoBan);
                cmd.Parameters.AddWithValue("moTa", _obj.MoTa);
                cmd.Parameters.AddWithValue("lastId", MySqlDbType.Int32);
                cmd.Parameters["lastId"].Direction = ParameterDirection.Output;

                if (con.State != ConnectionState.Open)
                    con.Open();
                result = cmd.ExecuteNonQuery();
                int i = Convert.ToInt32(cmd.Parameters["lastId"].Value);
                if (_obj.MaGoiKhoa == -1)
                    _obj.MaGoiKhoa = i;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return result;
        }

        public int deleteGoiKhoa(int _id)
        {
            int result = 0;
            try
            {
                if (_id == -1)
                    return 0;
                MySqlCommand cmd = new MySqlCommand("goi_khoa_delete", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("maGoiKhoa", _id);
                cmd.Parameters["maGoiKhoa"].Direction = ParameterDirection.Input;

                if (con.State != ConnectionState.Open)
                    con.Open();
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return result;
        }

        public int updateGoiKhoa(GoiKhoaEntity _obj)
        {
            int result = 0;
            try
            {
                if (_obj.MaGoiKhoa == -1)
                    return addGoiKhoa(_obj);

                MySqlCommand cmd = new MySqlCommand("lop_hoc_update", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("maGoiKhoa", _obj.MaGoiKhoa);
                cmd.Parameters.AddWithValue("tenGoiKhoa", _obj.TenGoiKhoa);
                cmd.Parameters.AddWithValue("maLopHoc", _obj.MaLopHoc);
                cmd.Parameters.AddWithValue("soBuoi", _obj.SoBuoi);
                cmd.Parameters.AddWithValue("thoiGian", _obj.ThoiGian);
                cmd.Parameters.AddWithValue("hocPhiCoBan", _obj.HocPhiCoBan);
                cmd.Parameters.AddWithValue("moTa", _obj.MoTa);

                if (con.State != ConnectionState.Open)
                    con.Open();
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return result;
        }
    }
}
